﻿using ClosedXML.Excel;
using FinScreen.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using FinScreen.Util;

namespace FinScreen
{
    class MasterBase : Observable<Instrument>, IEnumerable<Instrument>
    {
        private readonly List<Instrument> _instruments = new List<Instrument>();

        public Instrument this[string code]
        {
            get { return _instruments.SingleOrDefault(p => string.Equals(p.Code, code, StringComparison.InvariantCultureIgnoreCase)); }
        }

        public void Load()
        {
            LoadOne("ss.a.xlsx", "SS");
            //LoadOne("sz.a.xlsx", "SZ");
        }

        private void LoadOne(string name, string exchange)
        {
            string path = System.IO.Path.Combine(Thread.GetDomain().BaseDirectory, @"Data\" + name);
            using (var workbook = new XLWorkbook(path))
            {
                var sheet = workbook.Worksheet(1);
                foreach (var row in sheet.Rows())
                {
                    var code = row.Cell("A").Value.ToString();
                    if (code == "代码" || string.IsNullOrEmpty(code)) continue;

                    var instrument = new Instrument
                        {
                        Code = (code + "." + exchange).ToUpper(),
                        Name = row.Cell("B").Value.ToString(),
                        Industry = row.Cell("S").Value.ToString(),
                        PEG = row.Cell("P").Value.ToString().ToDouble(),

                        LastPrice = row.Cell("D").Value.ToString().ToDouble()??0,
                        Close = row.Cell("D").Value.ToString().ToDouble() ?? 0,
                        PrevClose = row.Cell("O").Value.ToString().ToDouble() ?? 0,
                        High = row.Cell("M").Value.ToString().ToDouble() ?? 0,
                        Low = row.Cell("N").Value.ToString().ToDouble() ?? 0,
                        NeiPan = row.Cell("W").Value.ToString().ToInt() ?? 0,
                        WaiPan = row.Cell("X").Value.ToString().ToInt() ?? 0,
                        NeiWaiBi = row.Cell("Y").Value.ToString().ToDouble() ?? 0,
                        NetChange = row.Cell("E").Value.ToString().ToDouble() ?? 0,
                        PctChange = row.Cell("C").Value.ToString().ToDouble() ?? 0,
                        Open = row.Cell("L").Value.ToString().ToDouble() ?? 0,
                        TurnOver = row.Cell("Q").Value.ToString().ToDouble() ?? 0,
                        Volume = row.Cell("H").Value.ToString().ToDouble() ?? 0
                    };

                    _instruments.Add(instrument);

                    OnNext(instrument);
                }
            }
        }

        public IEnumerator<Instrument> GetEnumerator()
        {
            return _instruments.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _instruments.GetEnumerator();
        }
    }
}